package com.yuandian.bpm.bi.modules.quartz.util;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.yuandian.bpm.bi.modules.quartz.service.impl.JobReportPushImpl;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskInfo;
import com.yuandian.bpm.bi.modules.quartz.vo.RedisMsgParam;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * <p> 操作任务工具类 </p>
 *
 * @Author: LinShiHe
 * @Date: 2022/10/12 23:40
 */
@Component
@Slf4j
public class QuartzUtil {

    @Autowired
    private JobUtil jobUtils;

    private static JobUtil jobUtilsStatic;


    @PostConstruct
    public void init() {
        jobUtilsStatic = this.jobUtils;
    }



    /**
     * 根据操作命令对任务进行操作
     * @param message   删除/暂停任务：task|delete|任务ID
     */
    public static void operationalTasks(String message) {
        // 根据命令封装成实体类
        RedisMsgParam redisMsgParam = getRedisMsgParam(message);

		if ("delete".equalsIgnoreCase(redisMsgParam.getTaskOperator())) {
			// 删除任务
			deleteJob(redisMsgParam);
		}

    }

    /**
     * 删除任务
     * @param redisMsgParam	消息参数
     */
	private static void deleteJob(RedisMsgParam redisMsgParam) {

		ConfReportTaskInfo quartzBean = new ConfReportTaskInfo();
        quartzBean.setId(redisMsgParam.getTaskId());
        quartzBean.setJobName(String.valueOf(redisMsgParam.getTaskId()));
        quartzBean.setJobGroup(String.valueOf(redisMsgParam.getTaskId()));
        quartzBean.setTriggerName(String.valueOf(redisMsgParam.getTaskId()));
        quartzBean.setTriggerGroup(String.valueOf(redisMsgParam.getTaskId()));

        log.info("[删除任务][任务ID:{}][任务名称:{}, 任务分组:{}][触发器名称:{},触发器分组:{}]",
                quartzBean.getId(),
                quartzBean.getJobName(),
                quartzBean.getJobGroup(),
                quartzBean.getTriggerName(),
                quartzBean.getTriggerGroup());

        jobUtilsStatic.cancelJob(quartzBean);
    }

    /**
     * 添加任务
     * @param confReportTaskInfo 任务参数
     */
	public void addJob(ConfReportTaskInfo confReportTaskInfo) {
		if (confReportTaskInfo.getId() == null) {
			log.info("[添加任务]-失败: 任务id为空");
			return;
		}

		// 添加任务参数
		int taskId = confReportTaskInfo.getId();
		confReportTaskInfo.setJobName(String.valueOf(taskId));
		confReportTaskInfo.setJobGroup(String.valueOf(taskId));
		confReportTaskInfo.setTriggerName(String.valueOf(taskId));
		confReportTaskInfo.setTriggerGroup(String.valueOf(taskId));

		// 添加任务
		JobDataMap jobDataMap = new JobDataMap();
		jobDataMap.put("confReportTaskInfo", confReportTaskInfo);
		jobUtilsStatic.addJob(JobReportPushImpl.class, jobDataMap, confReportTaskInfo);

		log.info("[添加任务][任务ID:{}][任务名称:{}, 任务分组:{}][触发器名称:{},触发器分组:{}][cron:{}]",
				confReportTaskInfo.getId(),
				confReportTaskInfo.getTaskName(),
				confReportTaskInfo.getJobGroup(),
				confReportTaskInfo.getTriggerName(),
				confReportTaskInfo.getTriggerGroup(),
				confReportTaskInfo.getTaskCronExpression());
    }



	/**
	 * 根据命令封装成实体类
	 * 			删除/暂停任务：task|delete|任务ID
	 */
	private static RedisMsgParam getRedisMsgParam(String message) {
		if (StringUtils.isNotEmpty(message)) {
			// 把 message 中的引号替换掉
			message = message.replaceAll("\"","");
			String[] split = message.split("\\|");
			if (split.length == 3) {
				String taskOperator = split[1];
				int taskId = Integer.parseInt(split[2]);
				return new RedisMsgParam(taskOperator, taskId);
			}
		}
		return new RedisMsgParam();
	}

}
